home *** CD-ROM | disk | FTP | other *** search
Wrap
/* File: Test.c Contains: xxx put contents here xxx Written by: Kevin Arnold - based on sample code from Nav services written by Tony Bacigalupi Copyright: © 1998 by Apple Computer, Inc., all rights reserved. Change History (most recent first): <5> 8/25/98 KA we weren't setting done to false in the beginning of TestSyncServicesLookup <4> 8/12/98 KA added cleanup calls to free our NSL made objects <3> 7/21/98 KA added NSLErrorToString calls <1> 4/06/98 KA Initial check-in. To Do: */ #include "NSLAPI.h" #if PROFILE #include <Profiler.h> #endif #include <StdIO.h> #include <string.h> #ifdef __MWERKS__ #include <SIOUX.h> #endif #define kWebServerType "http" #define kFTPServerType "ftp" #define kAppleShareType "AFPServer" #define kLaserWriterType "LaserWriter" NSLClientRef gOurClientRef; char gErrorString[256]; char gSolutionString[256]; void TestDefaultNeighborhoodLookup( void ); void TestSyncNeighborhoodLookup( NSLNeighborhood neighborhood ); void TestSyncServicesLookup( char* service ); static char GetPressedKey() { char c; char dummy; scanf("%c%c", &c, &dummy); return c; } void main() { OSStatus status; Boolean quit = false; Boolean doBanner = true; #ifdef __MWERKS__ SIOUXSettings.asktosaveonclose = 0; #endif #if PROFILE ProfilerInit(collectDetailed, bestTimeBase, 500, 20); #endif printf("NSL API Test\rGenerated %s at %s\r\r", __DATE__, __TIME__); status = NSLOpenNavigationAPI( &gOurClientRef ); if ( status && status != kNSLSomePluginsFailedToLoad ) printf("NSLAPI could not be opened due to an internal error\r"); else { if ( status == kNSLSomePluginsFailedToLoad ) { // this isn't a fatal error, so we want to let someone know but we will continue. NSLError cludgeError; // we have to crock one as this is the only time we will get back an OSStatus // instead of an NSLError. Just make theContext be zero cludgeError.theErr = status; cludgeError.theContext = 0; NSLErrorToString( cludgeError, gErrorString, gSolutionString ); printf("Error #%ld: %s\r", cludgeError.theErr, gErrorString ); printf("Solution: %s\r", gSolutionString ); } do { char c; if ( doBanner ) printf("\r\r\rPlease make a choice [1-6 and hit return]:\r\t1 - Get list of Default Neighborhoods available\r\t2 - Get list of Webservers in a Neighborhood\r\t3 - Get list of ftp servers in a Neighborhood\r\t4 - Get list of AppleShare servers in a Neighborhood\r\t5 - Get list of LaserWriters in a Neighborhood\r\t6 - QUIT\r\r> "); c = GetPressedKey(); doBanner = true; switch ( c) { case '1': TestDefaultNeighborhoodLookup(); break; case '2': TestSyncServicesLookup(kWebServerType); break; case '3': TestSyncServicesLookup(kFTPServerType); break; case '4': TestSyncServicesLookup(kAppleShareType); break; case '5': TestSyncServicesLookup(kLaserWriterType); break; case '6': printf("Bye now!\r"); quit = true; break; default: doBanner = false; } } while ( quit == false ); } #if PROFILE ProfilerDump("\pPerformances"); ProfilerTerm(); #endif ExitToShell(); } #define kBufferLength 4096 void TestDefaultNeighborhoodLookup( void ) { NSLNeighborhood neighborhood; printf( "Getting default NSLNeighborhoods\r\r" ); neighborhood = NSLMakeNewNeighborhood( "", NULL ); // empty string for default neighborhood lookup, NULL for empty protocol string // meaning we want all default neighborhoods TestSyncNeighborhoodLookup( neighborhood ); NSLFreeNeighborhood( neighborhood ); // make sure we free up this memory } void TestSyncNeighborhoodLookup( NSLNeighborhood neighborhood ) { char tempName[256]; long bufLen = kBufferLength; char* buffer = NULL; char* tempPtr = NULL; NSLRequestRef ourRequestRef; ClientAsyncInfoPtr ourAsyncInfo; NSLError iErr = kNSLErrorNoErr; NSLNeighborhood nhPtr = NULL; long nhLength, tempPtrLength; Boolean done = false; printf( "Looking up Neighborhoods\r\r" ); buffer = NewPtr( bufLen ); // first prepare the request which will set up a ClientAsyncInfoPtr for us iErr = NSLPrepareRequest( NULL, NULL, gOurClientRef, &ourRequestRef, buffer, bufLen, &ourAsyncInfo ); if ( iErr.theErr ) { printf("NSLPrepareRequest returned error %ld\r", iErr.theErr ); NSLErrorToString( iErr, gErrorString, gSolutionString ); printf("Error: %s\r", gErrorString ); printf("Solution: %s\r", gSolutionString ); } // set the values of ourAsyncInfo pb ourAsyncInfo->clientContextPtr = NULL; ourAsyncInfo->maxSearchTime = 0; // no max search time ourAsyncInfo->alertInterval = 0; // no alert interval ourAsyncInfo->alertThreshold = 1; // make alert threshold every item... if ( iErr.theErr == noErr ) iErr = NSLStartNeighborhoodLookup( ourRequestRef, neighborhood, ourAsyncInfo ); do { if ( iErr.theErr == noErr && ourAsyncInfo->totalItems > 0 ) { while ( NSLGetNextNeighborhood( ourAsyncInfo, &nhPtr, &nhLength ) ) { if ( nhLength > 0 && nhLength < kBufferLength ) { NSLGetNameFromNeighborhood( nhPtr, &tempPtr, &tempPtrLength ); memcpy( tempName, tempPtr, tempPtrLength ); tempName[tempPtrLength] = '\0'; printf( "%s\r", tempName ); } } } if ( ourAsyncInfo->searchState == kNSLSearchStateComplete ) done = true; else if ( iErr.theErr == noErr ) iErr = NSLContinueLookup( ourAsyncInfo ); } while ( !iErr.theErr && !done ); if ( iErr.theErr ) { NSLErrorToString( iErr, gErrorString, gSolutionString ); printf("Error: %s\r", gErrorString ); printf("Solution: %s\r", gSolutionString ); } if ( buffer ) DisposePtr(buffer); } void TestSyncServicesLookup( char* service ) { char name[256] = "\p"; char url[1024]; long bufLen = kBufferLength; char* buffer = NewPtr( bufLen ); NSLRequestRef ourRequestRef; ClientAsyncInfoPtr ourAsyncInfo; NSLError iErr = kNSLErrorNoErr; NSLServicesList serviceList = NULL; TypedDataPtr newDataPtr = NULL; NSLNeighborhood neighborhood, urlPtr = NULL; long urlLength; Boolean done = false; char c; printf( "Please Type in the neighborhood you want to search then hit return\r" ); scanf("%c", &c ); while ( c != '\r' && c != '\n' && name[0] < 256 ) { name[0]++; name[name[0]] = c; scanf("%c", &c ); }; p2cstr( (unsigned char*)name ); neighborhood = NSLMakeNewNeighborhood( name, NULL ); printf( "Looking up Service\r\r" ); // first prepare the request which will set up a ClientAsyncInfoPtr for us iErr = NSLPrepareRequest( NULL, NULL, gOurClientRef, &ourRequestRef, buffer, bufLen, &ourAsyncInfo ); if ( iErr.theErr ) { printf("NSLPrepareRequest returned error %ld\r", iErr.theErr ); NSLErrorToString( iErr, gErrorString, gSolutionString ); printf("Error #%ld: %s\r", iErr.theErr, gErrorString ); printf("Solution: %s\r", gSolutionString ); } else { serviceList = NSLMakeNewServicesList( service ); // we can pass a comma delimited cstring here (ie "http,ftp") // now we need to create a TypedDataPtr which holds teh serviceList info as well as an attribute if we wish if ( serviceList != NULL ) { iErr.theErr = NSLMakeRequestPB( serviceList, "", &newDataPtr ); // we can also pass an attribute if we want to get more specific NSLDisposeServicesList( serviceList ); // we are done with this, free this memory! } // set the values of ourAsyncInfo pb ourAsyncInfo->clientContextPtr = NULL; ourAsyncInfo->maxSearchTime = 0; // no max search time ourAsyncInfo->alertInterval = 0; // no alert interval ourAsyncInfo->alertThreshold = 1; // make alert threshold every item... if ( iErr.theErr == noErr ) iErr = NSLStartServicesLookup( ourRequestRef, neighborhood, newDataPtr, ourAsyncInfo ); do { if ( iErr.theErr == noErr && ourAsyncInfo->totalItems > 0 ) { while ( NSLGetNextUrl( ourAsyncInfo, &urlPtr, &urlLength ) ) { if ( urlLength > 0 ) { memcpy( url, urlPtr, urlLength ); url[urlLength] = '\0'; printf( "%s\r", url ); } } } else if ( iErr.theErr ) { NSLErrorToString( iErr, gErrorString, gSolutionString ); printf("Error #%ld: %s\r", iErr.theErr, gErrorString ); printf("Solution: %s\r", gSolutionString ); } if ( ourAsyncInfo->searchState == kNSLSearchStateComplete ) done = true; else if ( iErr.theErr == noErr ) iErr = NSLContinueLookup( ourAsyncInfo ); } while ( !done ); // it is possible to get errors back, but if the search state isn't complete, then one or more // plugins are still working! NSLFreeTypedDataPtr( newDataPtr ); // dispose this } NSLFreeNeighborhood( neighborhood ); // make sure we free up this memory if ( buffer ) DisposePtr(buffer); }